<?php
/**
 * 订单管理 2016-4-12
 * 外包
 */
namespace AdminV3\Controller;

use Think\Page;


class OrderController extends CommonController
{

    public function index()
    {
        $code = trim(I('get.code'));
        $code_status = trim(I('get.code_status'));

        $p = empty($_GET['p']) ? 1 : $_GET['p'];

        $o = M('Order');
        $od = M('Orderdetial');

        if ($code_status == '' || $code_status == '1') {
            $list = $o->page($p . ',' . session('page'))->select();
            $count = $o->count();
        }
        if ($code_status == '2' || $code_status == '3' || $code_status == '4' || $code_status == '5' || $code_status == '6') {

            $list = $o->where('code_status=' . $code_status)->page($p . ',' . session('page'))->select();
            $count = $o->where('code_status=' . $code_status)->count();
        }
        if ($code != '') {

            $list = $o->where('code=' . $code)->page($p . ',' . session('page'))->select();
            $count = $o->where('code=' . $code)->count();
        }


        $result = array();

        foreach ($list as $k => $v) {
            $tar = $v;
            $odlist = $od->where(array('order_id' => $v['id']))->select();
            foreach ($odlist as $x => $y) {
                $tar['orderdetial'][$y['jxs_id']][] = $y;
            }
            //$tar['orderdetial'] = $odlist;
            $result[] = $tar;
        }

        //dump($result);die();

        $Page = new Page($count, session('page'));

        $map['code'] = $code;
        $map['code_status'] = $code_status;

        foreach ($map as $key => $val) {
            $Page->parameter[$key] = urlencode($val);
        }
        $show = $Page->show();
        $this->assign('page', $show);
        $this->assign('list', $result);
        $this->display();

    }

    public function delivery()
    {
        $id = I('id');
        $data = array(
            'wuliu_code' => I('wuliu_code'),
            'wuliu_company' => I('wuliu_company'),
            'wuliu_status' => 3,
            'code_status' => 4,
            'wuliu_time' => time(),

        );
        if (empty($id)) {
            $this->_ajaxreturn(false, '缺少必要参数');
        }
        if (empty($data['wuliu_code']) || empty($data['wuliu_company'])) {
            $this->_ajaxreturn(false, '不能为空');
        }
        $Model = D('Order');
        $rs = $Model->where('id=' . $id)->save($data);
        if ($rs) {
            $this->_ajaxreturn(true, '修改完成');
        } else {
            $this->_ajaxreturn(false, '修改失败，请稍后重试');
        }
    }

    public function uplivery()
    {
        $id = I('id');
        $data = array(
            'r_username' => I('r_username'),
            'r_phone' => I('r_phone'),
            'r_addr' => I('r_addr'),

        );
        if (empty($id)) {
            $this->_ajaxreturn(false, '缺少必要参数');
        }
        if (empty($data['r_username']) || empty($data['r_phone']) || empty($data['r_addr'])) {
            $this->_ajaxreturn(false, '不能为空');
        }
        $Model = D('Order');
        $rs = $Model->where('id=' . $id)->save($data);
        if ($rs) {
            $this->_ajaxreturn(true, '修改完成');
        } else {
            $this->_ajaxreturn(false, '修改失败，请稍后重试');
        }
    }

    public function refund()
    {
        $id = I('id');
        $data = array(
            'refund_money' => I('refund_money'),
            'refund_type' => I('refund_type'),
            'refund_wuliu' => I('refund_wuliu'),
            'refund_time' => time(),
            'refund_wuliu_code' => I('refund_wuliu_code'),
            'refund_ds' => I('refund_ds'),


        );
        if (empty($id)) {
            $this->_ajaxreturn(false, '缺少必要参数');
        }
        if (empty($data['refund_money']) || empty($data['refund_type']) || empty($data['refund_wuliu'])) {
            $this->_ajaxreturn(false, '不能为空');
        }
        $Model = D('Order');
        $rs = $Model->where('id=' . $id)->save($data);
        if ($rs) {
            $this->_ajaxreturn(true, '修改完成');
        } else {
            $this->_ajaxreturn(false, '修改失败，请稍后重试');
        }
    }

    public function gb()
    {
        $id = I('post.id');
        $data['status'] = 3;

        $rs = M('Order')->where('id=' . $id)->save($data);

        if ($rs) {
            $this->_ajaxreturn(true, '修改完成');
        } else {
            $this->_ajaxreturn(false, '修改失败，请稍后重试');
        }
    }

    public function alter()
    {
        $idList = I('post.idlist');
        $type = I('post.type');
        $result = array();
        if (!empty($type)) {
            if (!empty($idList)) {
                $idList = rtrim($idList, ',');
                //var_dump($idList);
                $idList = explode(',', $idList);
                //var_dump($idList);
                $data['ok'] = 0;
                $data['error'] = 0;

                foreach ($idList as $k => $val) {
                    //通过审核
                    switch ($type) {


                        case 'restore':
                            $newType = 1;
                            break;
                        case 'cancel':
                            $newType = 2;
                            break;
                        case 'del':
                            $newType = 3;
                            break;
                    }

                    $re = M('Order')->where('id=' . $val)->setField('status', $newType);
                    if ($re > 0) {
                        $data['ok']++;
                    } else {
                        $data['error']++;
                    }
                }
                $result['mes'] = "操作成功:{$data['ok']},失败{$data['error']}";
                $this->ajaxReturn($result);

            } else {
                $result['mes'] = '没有选择的值';
                $this->ajaxReturn($result);
            }
        } else {
            $result['mes'] = '非法操作!';
            $this->ajaxReturn($result);
        }

    }


    public function setMessage()
    {
        $code_id = I('post.code_id');
        $orderData=M('Order')->where('id='.$code_id)->find();

        if($orderData){

            $return_data['status']='1';
            $return_data['info']='信息发送成功!';
        }else{
            $return_data['status']='2';
            $return_data['info']='访问的订单不存在!';
        }
        $this->ajaxReturn($return_data);

    }

    function _ajaxreturn($s=true,$m='Succes',$d=array()){
        exit(json_encode(array('s'=>$s,'m'=>$m,'d'=>$d)));
    }


}